//
//  SheetMenuView.m
//  one_mile_01
//
//  Created by 王雅蓉 on 15/10/30.
//  Copyright © 2015年 王雅蓉. All rights reserved.
//

#import "SheetMenuView.h"
#import "SheetMenuTableViewCell.h"

@implementation SheetMenuView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createSubviews];
    }
    return self;
}

-(void)createSubviews
{
    self.backgroundColor = [UIColor colorWithRed:160 / 255.0 green:160 / 255.0 blue:160 / 255.0 alpha:0];
    
    self.sheetMenuTV = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT, WIDTH, 60 * FITRATE * 3 + 10 * FITRATE) style:UITableViewStylePlain];
    self.sheetMenuTV.dataSource = self;
    self.sheetMenuTV.delegate = self;
    self.sheetMenuTV.scrollEnabled = NO;
    
    [self addSubview:_sheetMenuTV];
    [self disappearAction];
}

-(void)disappearAction
{
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToCancel)];
    [self addGestureRecognizer:tapGR];
    
    tapGR.delegate = self;
    
    [UIView animateWithDuration:0.25f animations:^{
        
        self.backgroundColor = [UIColor colorWithRed:160 / 255.0 green:160 / 255.0 blue:160 / 255.0 alpha:0.4];
        
        [UIView animateWithDuration:0.25f animations:^{
            
            _sheetMenuTV.frame = CGRectMake(_sheetMenuTV.frame.origin.x, HEIGHT - _sheetMenuTV.frame.size.height, _sheetMenuTV.frame.size.width, _sheetMenuTV.frame.size.height);
        }];
    } completion:^(BOOL finished) {
        
    }];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isKindOfClass:[self class]]) {
        
        return YES;
    }
    return NO;
}

-(void)tapToCancel
{
    [UIView animateWithDuration:0.25f animations:^{
        
        self.sheetMenuTV.frame = CGRectMake(0, HEIGHT, WIDTH, 0);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

-(void)showSheetMenuView:(UIViewController *)sheetView
{
    if (sheetView == nil) {
        [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:self];
    } else {
        [sheetView.view addSubview:self];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 10 * FITRATE;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60 * FITRATE;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *sheetCellIdentifier = @"sheetMenuCell";
    
    SheetMenuTableViewCell *sheetCell = [tableView dequeueReusableCellWithIdentifier:sheetCellIdentifier];
    
    if (sheetCell == nil) {
        sheetCell = [[SheetMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sheetCellIdentifier];
        sheetCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            sheetCell.sheetTitleL.font = [UIFont systemFontOfSize:14.0f * FITRATE];
            sheetCell.sheetTitleL.textColor = [UIColor colorWithRed:112 / 255.0 green:112 / 255.0 blue:112 / 255.0 alpha:1.0];
            sheetCell.sheetTitleL.text = @"退出后不会删除任何历史数据，下次登录依然可以使用本账号。";
        } else {
            
            sheetCell.sheetTitleL.font = [UIFont systemFontOfSize:18.0f * FITRATE];
            sheetCell.sheetTitleL.textColor = [UIColor redColor];
            sheetCell.sheetTitleL.text = @"退出登录";
        }
    } else {
        
        sheetCell.sheetTitleL.font = [UIFont systemFontOfSize:18.0f * FITRATE];
        sheetCell.sheetTitleL.textColor = [UIColor blackColor];
        sheetCell.sheetTitleL.text = @"取消";
    }
    
    return sheetCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self tapToCancel];
    
    if (_myDelegate != nil && [_myDelegate respondsToSelector:@selector(didSelectMenuIndex:)]) {
        [_myDelegate didSelectMenuIndex:indexPath];
        return;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
